/*
 * $Id: CategoryNodeProgrameTempManager.java 8 2012-04-28 05:11:23Z forwardly@gmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.service.planfirst;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.as.dao.category.CategoryLeafDao;
import com.lightsaber.trade.as.dao.category.CategoryNodeDao;
import com.lightsaber.trade.as.dao.planning.CategoryNodeProgrameTempDao;
import com.lightsaber.trade.as.dao.planning.ProgrammePlanningItemDao;
import com.lightsaber.trade.as.dao.programme.ProgrammeDao;
import com.lightsaber.trade.as.entity.category.Category;
import com.lightsaber.trade.as.entity.category.CategoryLeaf;
import com.lightsaber.trade.as.entity.category.CategoryNode;
import com.lightsaber.trade.as.entity.planning.CategoryNodeProgrameTemp;
import com.lightsaber.trade.as.entity.planning.Level1Planning;
import com.lightsaber.trade.as.entity.planning.Level2Planning;
import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Transactional
public class CategoryNodeProgrameTempManager extends
        BaseServiceImpl<CategoryNodeProgrameTemp, String> {

    @Autowired
    CategoryNodeProgrameTempDao categoryNodeProgrameTempDao;

    @Autowired
    CategoryNodeDao categoryNodeDao;

    @Autowired
    ProgrammeDao programmeDao;
    @Autowired
    CategoryLeafDao categoryLeafDao;

    // second level planning
    @Autowired
    ProgrammePlanningItemDao programmePlanningItemDao;

    @Override
    protected BaseDao<CategoryNodeProgrameTemp, String> getDao() {
        return categoryNodeProgrameTempDao;
    }

    public List<Object[]> group(final Level1Planning level1Planning,
            final CategoryNode disciplineTargetNode) {
        return categoryNodeProgrameTempDao.group(level1Planning, disciplineTargetNode);
    }

    public void clear(final Level1Planning level1Planning, final CategoryNode disciplineTargetNode) {
        categoryNodeProgrameTempDao.clear(level1Planning, disciplineTargetNode);
    }

    private void saveProgrameAsMe(final Level1Planning level1Planning,
            final Level2Planning level2Planning, final CategoryLeaf categoryLeaf,
            final CategoryNode disciplineTargetNode) {

        Category category = level1Planning.getCategory();

        List<Programme> programmeList = programmeDao.filterByAttributes(category, categoryLeaf);

        for (Programme programme : programmeList) {
            CategoryNodeProgrameTemp temp = new CategoryNodeProgrameTemp();

            temp.setDiscipline(programme.getDiscipline());
            temp.setFundingMode(programme.getFundingMode());

            long planplaces = programmePlanningItemDao.sumPlanplaceByPlanningAndCode(
                    level2Planning, programme.getCode());
            temp.setPlannedProgressing(planplaces);

            temp.setProgramme(programme);
            temp.setDisciplineTargetNode(disciplineTargetNode);
            temp.setLevel1Planning(level1Planning);

            categoryNodeProgrameTempDao.save(temp);
        }
    }

    public void saveProgrameAsMe(final Level1Planning level1Planning,
            final Level2Planning level2Planning, final CategoryNode categoryNode,
            final CategoryNode disciplineTargetNode) {

        if (categoryNode.getIsLeafNode() == 1) {
            CategoryLeaf leaf = categoryLeafDao.get(categoryNode.getId());
            saveProgrameAsMe(level1Planning, level2Planning, leaf, disciplineTargetNode);
        } else {
            List<CategoryNode> children = categoryNode.getChildren();
            for (CategoryNode child : children) {
                saveProgrameAsMe(level1Planning, level2Planning, child, disciplineTargetNode);
            }
        }
    }
}
